home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Util / conv / Acvt.lha / Acvt 1.07 / sources / switches.bat < prev    next >
DOS Batch File  |  1999-06-14  |  5KB  |  321 lines

  1. @rem = '--*-Perl-*--';
  2. @rem = '
  3. @echo off
  4. perl -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endperl
  6. @rem ';
  7.  
  8. ############################################################################
  9. ## Script for generating switches.cpp
  10. ##
  11. ############################################################################
  12.  
  13. open INFILE, "switches.def" or die;
  14. @inf = <INFILE>;
  15. close INFILE;
  16.  
  17. unshift @inf, "=";
  18. unshift @inf, "=bRet = SWFN_HELP( USAGE );";
  19. unshift @inf, "this help";
  20. unshift @inf, "help, ?";
  21.  
  22. push @out, <<LOOP_START;
  23.  
  24. BOOL SWITCHES_Init( int* pargc, char** argv )
  25. {
  26. BOOL bRet = TRUE;
  27.  
  28. m_iArgsCurr = *pargc - 1;
  29. m_iArgsOrig = 1;
  30. m_ppcArgvOrig = argv + 1;
  31. m_ppcArgvCurr = argv + 1;
  32.  
  33. while( m_iArgsCurr )
  34. {
  35. if ( !bRet )
  36.     break;
  37.  
  38. if ( SWITCHES_GetValue() )
  39. {
  40. bRet = TRUE;
  41. *( m_ppcArgvOrig++ ) = * ( m_ppcArgvCurr - 1 );
  42. m_iArgsOrig++;
  43. }
  44. else
  45. {
  46. m_szSwitch++;
  47.  
  48. bRet = TRUE;
  49.  
  50. LOOP_START
  51.  
  52. for(;;)
  53. {
  54.     $line = shift @inf;
  55.  
  56.     last unless defined( $line );
  57.  
  58.     chomp $line;
  59.  
  60.     $line =~ s/\s+$//;
  61.     $line =~ s/^\s+//;
  62.  
  63.     next unless $line;
  64.  
  65.     unshift @inf, $line;
  66.  
  67.     &switch();
  68.  
  69. };
  70.  
  71. push @out, <<LOOP_END;
  72.  
  73. printf( "Invalid switch: %s\\n", *( m_ppcArgvCurr - 1 ) );
  74. return FALSE;
  75. } //endif
  76. } //while
  77.  
  78. *pargc = m_iArgsOrig;
  79. return bRet;
  80. }
  81. LOOP_END
  82.  
  83. if ( $CODE_INCLUDE{ 'SWFN_GETPATH' } )
  84. {
  85.     unshift @out, <<SWFN_GETPATH_CODE;
  86.  
  87. BOOL SWFN_GETPATH( char* szPath )
  88. {
  89. char* szOrigSw = m_szSwitch;
  90.  
  91. if ( !SWITCHES_GetValue() )
  92. {
  93. printf( "Missing value for switch '%s'\\n", szOrigSw );
  94. return FALSE;
  95. }
  96.  
  97. strcpy( szPath, m_szSwitch );
  98. return TRUE;
  99. }
  100.  
  101. SWFN_GETPATH_CODE
  102. }
  103.  
  104. if ( $CODE_INCLUDE{ 'SWFN_NUMH' } )
  105. {
  106.     unshift @out, <<SWFN_NUMH_CODE;
  107.  
  108. BOOL SWFN_NUMH( int* piNum )
  109. {
  110. char* szOrigSw = m_szSwitch;
  111.  
  112. if ( !SWITCHES_GetValue() )
  113. {
  114. printf( "Missing value for switch '%s'\\n", szOrigSw );
  115. return FALSE;
  116. }
  117.  
  118. sscanf( m_szSwitch, "%lX", piNum );
  119.  
  120. return TRUE;
  121. }
  122. SWFN_NUMH_CODE
  123. }
  124.  
  125. unshift @out, <<SWFN_HELP_CODE2;
  126. return FALSE;
  127. }
  128.  
  129. SWFN_HELP_CODE2
  130.  
  131. $longest = 0;
  132.  
  133. foreach $line ( @help )
  134. {
  135.     my ( $switch, $desc, $par ) = split( /\|/, $line, 3 );
  136.  
  137.     my $len = length( $switch ) + length( $par );
  138.  
  139.     $longest = $len if ( $len > $longest );
  140. }
  141.  
  142. $longest += 2 + 9;
  143. foreach $line ( @help )
  144. {
  145.     my ( $switch, $desc, $par ) = split( /\|/, $line, 3 );
  146.  
  147.     my $len = length( $switch ) + length( $par );
  148.  
  149.     my $l = "printf( \"-$switch";
  150.  
  151.     if ( $par )
  152.     {
  153.         $l .= " $par";
  154.     }
  155.  
  156.     $l .= ' ' x ( $longest - length( $l ) );
  157.  
  158.     $l .= "  -$desc\\n\" );\n";
  159.  
  160.     push @helpf, $l;
  161.  
  162. }
  163.  
  164. unshift @out, @helpf;
  165.  
  166. unshift @out, <<SWFN_HELP_CODE1;
  167.  
  168. BOOL SWFN_HELP( char* szUsage )
  169. {
  170. if ( szUsage )
  171.     printf( "%s\\n", szUsage );
  172. SWFN_HELP_CODE1
  173.  
  174.  
  175. unshift @out, <<SWITCHES_GLOBALS;
  176.  
  177. int m_iArgsCurr;
  178. int m_iArgsOrig;
  179. char** m_ppcArgvOrig;
  180. char** m_ppcArgvCurr;
  181. char* m_szSwitch;
  182.  
  183. //gets one token from cmdline
  184. BOOL SWITCHES_GetSwitch()
  185. {
  186. if ( !m_iArgsCurr )
  187.     return FALSE;
  188.  
  189. m_szSwitch = *( m_ppcArgvCurr++ );
  190.  
  191. m_iArgsCurr--;
  192.  
  193. return TRUE;
  194. }
  195.  
  196. //gets one token, returns TRUE if value
  197. BOOL SWITCHES_GetValue()
  198. {
  199. if ( !SWITCHES_GetSwitch() )
  200.     return FALSE;
  201.  
  202. switch( *m_szSwitch )
  203. {
  204. case '-':
  205. case '/':
  206.     return FALSE;
  207. default:
  208.     return TRUE;
  209. }
  210. }
  211.  
  212. SWITCHES_GLOBALS
  213.  
  214. open OUTFILE, ">switches.cpp";
  215.  
  216. print OUTFILE "//THIS IS A GENERATED FILE. DO NOT EDIT!!!\n";
  217. print OUTFILE "//EDIT switches.def INSTEAD!\n\n";
  218.  
  219. $indent = 0;
  220.  
  221. $bigline = join( '', @out );
  222. @out = split( /\n/, $bigline );
  223.  
  224. foreach $line ( @out )
  225. {
  226.     $indent-- if ( $line =~ /}/ );
  227.  
  228.     print OUTFILE "\t" x $indent, $line, "\n";
  229.  
  230.     $indent++ if ( $line =~ /{/ );
  231.  
  232. }
  233.  
  234. close OUTFILE;
  235.  
  236. sub switch()
  237. {
  238.     my $switch_desc = "";
  239.  
  240.     my $switches_names = shift @inf;
  241.     my $switches_desc = shift @inf;
  242.  
  243.     chomp $switches_desc;
  244.  
  245.     my @switches = split( /,/, $switches_names );
  246.  
  247.     foreach $switch ( @switches )
  248.     {
  249.         $switch =~ s/\s+$//;
  250.         $switch =~ s/^\s+//;
  251.  
  252.         if ( defined ( $SWITCHES{ $switch } ) )
  253.         {
  254.             die "Duplicate switch $switch";
  255.         }
  256.  
  257.         $SWITCHES{ $switch } = 1;
  258.  
  259.     }
  260.  
  261.     $switches_count = @switches;
  262.  
  263.     $switch_desc = $switches[ 0 ] . "|" . $switches_desc . "|";
  264.  
  265.     push @out, "if (";
  266.  
  267.     my @asc;
  268.  
  269.     foreach $switch (@switches )
  270.     {
  271.         push @asc, "!strcmp( m_szSwitch, \"$switch\" )";
  272.     }
  273.  
  274.     push @out, join( " || ", @asc );
  275.  
  276.     push @out, ")";
  277.     
  278.     push @out, "\n{\n";
  279.  
  280.     my @switch_pars = ();
  281.  
  282.     for(;;)
  283.     {
  284.         my $par_desc = shift @inf;
  285.  
  286.         my ( $par, $code ) = split( /=/, $par_desc, 2 );
  287.  
  288.         $par =~ s/\s+$//;
  289.         $par =~ s/^\s+//;
  290.  
  291.         $code =~ s/\s+$//;
  292.         $code =~ s/^\s+//;
  293.  
  294.         last if ( !$par && !$code );
  295.  
  296.         if ( $code =~ /(SWFN_\w+)/ )
  297.         {
  298.             $CODE_INCLUDE{ $1 } = 1;
  299.         }
  300.  
  301.         if ( $par )
  302.         {
  303.             push @switch_pars, $par;
  304.         }
  305.  
  306.         push @out, $code . "\n";
  307.     }
  308.  
  309.     push @out, "continue;\n";
  310.  
  311.     $switch_desc .= join( ' ', @switch_pars );
  312.  
  313.     push @help, $switch_desc;
  314.  
  315.     push @out, "}\n";
  316.  
  317. }
  318.  
  319. __END__
  320. :endperl
  321.